﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;

namespace CRMG.Container.Views.Lookup
{
    public partial class FrmGrades : FrmLookupBase
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        LookupManager lookupMan;
        DataTable dtGrades;
        int pGrdId = 0;

        public FrmGrades()
            : this(-1)
        { }

        public FrmGrades(int gradeId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmGrades).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmGrades).Assembly);
                lookupMan = new LookupManager();
                dtGrades = lookupMan.GetGrades();
                pGrdId = gradeId;
                this.Text = formRes.GetString("FrmGrades.Caption");
                this.lblName.Text = formRes.GetString("FrmGrades.lblGrade");
                this.btnSave.Text = pGrdId != -1 ? formRes.GetString("Update") : formRes.GetString("Add");
                this.btnCancel.Text = formRes.GetString("Cancel");

                if (pGrdId != -1)
                {
                    this.txtName.Text = DataUtil.GetObjectWithSelectedColumn(dtGrades, "GradeName", pGrdId, "OBJID").ToStr();
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmGrades.Ctor.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2044,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2044)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmGrades.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            try
            {
                base.SaveClick(sender, e);

                int GrdId = DataUtil.GetObjectWithSelectedColumn(dtGrades, "GradeName", txtName.Text, "OBJID").ToInt();
                if (GrdId != pGrdId && GrdId > 0)
                {
                    ControlUtil.ShowWarningMessage(
                    messageRes.GetString("FrmGrades.Save.CompanyAlreadyExist"),
                    messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    Grade grd = new Grade() { GradeName = txtName.Text };
                    int Id = 0;
                    if (pGrdId == -1)
                    {
                        grd.IsActive = 1;
                        Id = lookupMan.AddGrade(grd);
                    }
                    else
                    {
                        grd.OBJID = pGrdId;
                        lookupMan.UpdateGrade(grd);
                    }
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = pGrdId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr(),
                        LogObject = pGrdId == -1 ? Id : pGrdId
                    };
                    transLogger.Insert();

                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                      messageRes.GetString("FrmGrades.Save.Success"),
                      messageRes.GetString("Info"));
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmGrades.Save.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2045,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2045)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmGrades.Save.Error"),
                      messageRes.GetString("Error"));
            }

        }

    }
}
